package com.haojiang.mh.ui.activity

import com.haojiang.mh.R
import com.haojiang.mh.cache.Const
import com.haojiang.mh.ui.base.BaseActivity
import com.haojiang.mh.utils.ActManager
import com.haojiang.mh.utils.SpUtil
import kotlinx.android.synthetic.main.activity_common_success.*
import kotlinx.coroutines.*

/**
 * 通用成功页
 *
 * Created by ZapFive on 2019-05-26.
 *
 * wuzhuang@mirahome.me
 */
class CommonSuccessActivity : BaseActivity() {

    private var entrance = -1

    override fun getLayoutId() = R.layout.activity_common_success

    override fun getTitleName(): String? = null

    override fun initActivity() {
        entrance = intent.getIntExtra(Const.EXTRA_DATA, -1)
    }

    override fun initView() {
        when (entrance) {
            Const.CONNECT_DEVICE_SUCCESS -> {
                tv_success.text = getString(R.string.txt_connect_device_success)
                aiv_success.setImageResource(R.drawable.ic_bluetooth_connect_success)
            }
            Const.SET_WIFI_SUCCESS -> {
                tv_success.text = getString(R.string.txt_config_wifi_success)
                aiv_success.setImageResource(R.drawable.ic_common_success)
            }
            Const.UPDATE_Firmware_SUCCESS -> {
                tv_success.text = getString(R.string.tip_update_success)
                aiv_success.setImageResource(R.drawable.ic_common_success)
            }
        }

        GlobalScope.launch {
            delay(1500L)
            withContext(Dispatchers.Main) {
                handleRedirect()
            }
        }
    }

    private fun handleRedirect() {
        when (entrance) {
            Const.CONNECT_DEVICE_SUCCESS -> {
                redirectNoParamAndFinish(WifiPasswordActivity::class.java)
            }
            Const.SET_WIFI_SUCCESS -> {
                if (SpUtil[Const.ORIGIN_DOUBLE_MODE, true]) {
                    redirectNoParamAndFinish(ChooseUserModeActivity::class.java)
                } else {
                    ActManager.redirectToBottomActivity()
                    context.finish()
                }
            }
            Const.UPDATE_Firmware_SUCCESS -> {
                ActManager.redirectToBottomActivity()
                context.finish()
            }
        }
    }
}